﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;
using VocabManager.ViewModel;

namespace VocabManager.View
{
    public partial class DeckView : UserControlBase  
    {
        DeckViewModel _deckViewModel;
   
       internal ApplicationBarIconButton _addNewButton;

       internal ApplicationBarIconButton _changeSideButton;
       internal ApplicationBarIconButton _nextButton;
       internal ApplicationBarIconButton _editButton;
       internal ApplicationBarIconButton _folderButton;
       internal ApplicationBarIconButton _previousButton;
       public DeckView()
        {
            InitializeComponent();


            _addNewButton = new ApplicationBarIconButton(new Uri(IconUrls.Add, UriKind.Relative));
            _addNewButton.Text = "new card";
            _addNewButton.Click += new EventHandler(_addNew_Click);
          


            _changeSideButton = new ApplicationBarIconButton(new Uri(IconUrls.Turn, UriKind.Relative));
            _changeSideButton.Text = "turn card";
            _changeSideButton.Click += new EventHandler(_changeSideButton_Click);

            _editButton = new ApplicationBarIconButton(new Uri(IconUrls.Edit, UriKind.Relative));
            _editButton.Text = "edit";
            _editButton.Click += new EventHandler(_editButton_Click);


            _nextButton = new ApplicationBarIconButton(new Uri(IconUrls.Next, UriKind.Relative));
            _nextButton.Text = "next card";
            _nextButton.Click += new EventHandler(_nextButton_Click);

            _folderButton = new ApplicationBarIconButton(new Uri(IconUrls.Folder, UriKind.Relative));
            _folderButton.Text = "main page";
            _folderButton.Click += new EventHandler(_folderButton_Click);

            _previousButton = new ApplicationBarIconButton(new Uri(IconUrls.Back, UriKind.Relative));
            _previousButton.Text = "previous card";
            _previousButton.Click += new EventHandler(_previousButton_Click);




        }

        void _previousButton_Click(object sender, EventArgs e)
        {
            _deckViewModel.PereviousCommand.Execute(null);
        }

        void _folderButton_Click(object sender, EventArgs e)
        {
            App.Instance.MainPage.LoadDeckListView();
        }

        void _changeSideButton_Click(object sender, EventArgs e)
        {
            _deckViewModel.CurrentItem.ChangeSideCommand.Execute(null);
        }

        void _editButton_Click(object sender, EventArgs e)
        {
            _deckViewModel.CurrentItem.EditCommand.Execute(null);
        }

        void _addNew_Click(object sender, EventArgs e)
        {
            _deckViewModel.AddNewCommand.Execute(null);
        }

        void _searchButton_Click(object sender, EventArgs e)
        {
            _deckViewModel.SearchCommand.Execute(null);
        }
        void SetViewModel(DeckViewModel deckViewModel)
        {
            _deckViewModel = deckViewModel;
            _deckViewModel.SearchCommand.CanExecuteChanged += new EventHandler(SearchCommand_CanExecuteChanged);

            _deckViewModel.AddNewCommand.CanExecuteChanged += new EventHandler(AddNewCommand_CanExecuteChanged);

        }

        void _nextButton_Click(object sender, EventArgs e)
        {
            _deckViewModel.NextCommand.Execute(null);
        }



       

        void AddNewCommand_CanExecuteChanged(object sender, EventArgs e)
        {
            _addNewButton.IsEnabled = _deckViewModel.AddNewCommand.CanExecute(null);
        }

        void SearchCommand_CanExecuteChanged(object sender, EventArgs e)
        {
           
        }

  

        protected override void OnLoad()
        {
            base.OnLoad();
            this.SetViewModel(this.DataContext as DeckViewModel);
            StoreAppBarButtons();
            _deckViewModel.CardItems.MoveCurrentTo(null);
            LoadChildBasedOnState();
            _deckViewModel.PropertyChanged +=new System.ComponentModel.PropertyChangedEventHandler(_deckViewModel_PropertyChanged);
            ContainerPage.ApplicationBar.Buttons.Clear();
            ContainerPage.ApplicationBar.Buttons.Add(_addNewButton);
            ContainerPage.ApplicationBar.Buttons.Add(_folderButton);
                
        }

        private void LoadNewChild(UserControlBase newView )
        {
            this.LayoutRoot.Children.Clear();


            this.LayoutRoot.Children.Add(newView);
        }
        void _deckViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentItem")
            {
                LoadChildBasedOnState();
            }
        }

        private void LoadChildBasedOnState()
        {
            if (_deckViewModel.CurrentItem == null)
                LoadNewChild(new DeckViewInfoMode());
            else
            {
                ItemView itemView = new ItemView();
                itemView.DataContext = _deckViewModel.CurrentItem;
                LoadNewChild(itemView);
            }
        }
        protected override void OnUnload()
        {
            base.OnUnload();
            _deckViewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_deckViewModel_PropertyChanged);
       
        }
    }

}
